package com.power.sifa.approve.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.plf.common.domain.PageDataResponse;
import com.plf.common.domain.ResponseDTO;
import com.plf.common.utils.ExcelUtil;
import com.power.sifa.approve.api.IApl_archiveFeign;
import com.power.sifa.approve.model.Apl_archive;
import com.power.sifa.approve.model.pageInfo.Apl_archivePageInfo;
import com.power.sifa.approve.service.Apl_archiveService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;



/**
 *   归档记录控制器
 */

//@Slf4j
//@PowerLog
@RestController
@Api(value = "Apl_archiveController",tags={"归档记录"})
public class Apl_archiveController implements IApl_archiveFeign {

    @Resource
    private Apl_archiveService apl_archiveService;

    
    @Override
    @ApiOperation("查询归档记录分页列表")
    @SaCheckPermission("apl_archive_list")
    public ResponseDTO list(Apl_archivePageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo参数不能为空");
        Page<Apl_archive> page = this.apl_archiveService.findAll(pageInfo);
        List list = new ArrayList();
        if (!page.isEmpty()) {
            for (Apl_archive apl_archive : page) {
                apl_archive.geneText();
                list.add(apl_archive);
            }
        }
        PageDataResponse p = new PageDataResponse(list, page.getTotalElements(), page.getSize(), pageInfo.getPageNumber()<=1 ? 1 : pageInfo.getPageNumber());
        return ResponseDTO.succData(p);
    }

    @Override
    @ApiOperation("查看归档记录")
    @SaCheckPermission("apl_archive_view")
    public ResponseDTO view(String id) {
        Assert.notNull(id, "id不能为空");
        Apl_archive apl_archive  = apl_archiveService.getById(id);
        if (apl_archive != null) {
            apl_archive.geneText();
        }
        return ResponseDTO.succData(apl_archive);
    }

    @Override
    @ApiOperation("删除归档记录")
    @SaCheckPermission("apl_archive_del")
    public ResponseDTO delete(String id) {
        Assert.notNull(id, "id不能为空");
        apl_archiveService.removeById(id);
        return ResponseDTO.succ();
    }

    @Override
    @ApiOperation("保存或修改归档记录")
    @SaCheckPermission("apl_archive_save")
    public ResponseDTO save(Apl_archive apl_archive) {
        Assert.notNull(apl_archive, "apl_archive不能为空");
        apl_archiveService.saveOrUpdate(apl_archive);
        return ResponseDTO.succ();
    }
    
    @Override
    @ApiOperation(value="批量删除归档记录")
    @SaCheckPermission("apl_archive_del")
    public ResponseDTO batchDel(String ids) {
        Assert.isTrue(StringUtils.hasText(ids), "ids不能为空");
        List<Integer> list = Arrays.asList(ids.split(",")).stream().map(s -> Integer.parseInt(s.trim())).collect(Collectors.toList());
        apl_archiveService.removeByIds(list);
        return ResponseDTO.succ();
    }
    
    @Override
    @ApiOperation(value="导出excel")
    @SaCheckPermission("apl_archive_list")
    public void output(HttpServletResponse response, Apl_archivePageInfo pageInfo) {
        Assert.notNull(pageInfo, "pageInfo不能为空");
        String title = "归档记录";
        String[] heads = {"数据库主键","申请id","申请流水号","申请人","被申请人","第三人","收案日期","结案日期","立案日期","保存期限","案件承办人","案由","案卷详情说明","纸质正卷册数","纸质正卷页数","纸质副卷册数","纸质副卷页数","纸质其他卷册数","纸质其他卷页数","电子正卷册数","电子正卷页数","电子副卷册数","电子副卷页数","电子其他卷册数","电子其他卷页数"};
        String[] fields = {"id","applyid","applycode","applicantname","targetname","thirdname","acceptancedate","closeingdate","caserecorddate","storagelimit","handlername","reason","detail","paperpositivebook","paperpositivepage","paperreversebook","paperreversepage","paperotherbook","paperotherpage","elepositivebook","elepositivepage","elereversebook","elereversepage","eleotherbook","eleotherpage"};
        Page<Apl_archive> page = this.apl_archiveService.findAll(pageInfo);
        ExcelUtil.exportXls(title, heads, fields, page.getContent(), response);
    }
            
    @Override
    @ApiOperation(value="导出excel模板")
    public void exportExcelTemplate(HttpServletResponse response) {
        String title = "归档记录";
        //id自增或list从表可自行删除，需与导入项一致
        String[] heads = {"数据库主键","申请id","申请流水号","申请人","被申请人","第三人","收案日期","结案日期","立案日期","保存期限","案件承办人","案由","案卷详情说明","纸质正卷册数","纸质正卷页数","纸质副卷册数","纸质副卷页数","纸质其他卷册数","纸质其他卷页数","电子正卷册数","电子正卷页数","电子副卷册数","电子副卷页数","电子其他卷册数","电子其他卷页数"};
        ExcelUtil.exportXls(title, heads, null, null, response);
    }
    
	@Override
	@ApiOperation(value="导入excel")
	public ResponseDTO importExcel(MultipartFile file) throws IOException {
        Assert.notNull(file, "file不能为空");
        //id自增或list从表可自行删除，需与下载的模板中导入项一致
        String[] fields = {"id","applyid","applycode","applicantname","targetname","thirdname","acceptancedate","closeingdate","caserecorddate","storagelimit","handlername","reason","detail","paperpositivebook","paperpositivepage","paperreversebook","paperreversepage","paperotherbook","paperotherpage","elepositivebook","elepositivepage","elereversebook","elereversepage","eleotherbook","eleotherpage"};
        List<Apl_archive> list = (List<Apl_archive>)ExcelUtil.Import(fields, file, Apl_archive.class);
        if(!CollectionUtils.isEmpty(list)) {
            this.apl_archiveService.saveAll(list);
        }
        return ResponseDTO.succ();
	}

    /**
     * 通过applyid查询归档数据
     * @param applyid
     * @return
     */
    @Override
    public ResponseDTO getArchiveByApplyid(String applyid) {
        Apl_archive apl_archive  = this.apl_archiveService.getArchiveByApplyid(applyid);
        if (apl_archive != null) {
            apl_archive.geneText();
        }
        return ResponseDTO.succData(apl_archive);
    }

}
